<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Sets up a DateInterval from the relative parts of the string</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="dateinterval.construct.html">« DateInterval::__construct</a></li>
      <li style="float: right;"><a href="dateinterval.format.html">DateInterval::format »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.dateinterval.html">DateInterval</a></li>
    <li>Sets up a DateInterval from the relative parts of the string</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="dateinterval.createfromdatestring" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DateInterval::createFromDateString</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DateInterval::createFromDateString</span> &mdash; <span class="dc-title">Sets up a DateInterval from the relative parts of the string</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-dateinterval.createfromdatestring-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><strong>DateInterval::createFromDateString</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$datetime</code></span>): <span class="type"><span class="type"><a href="class.dateinterval.html" class="type DateInterval">DateInterval</a></span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   Uses the date/time parsers as used in the
   <span class="classname"><a href="class.datetimeimmutable.html" class="classname">DateTimeImmutable</a></span> constructor to create a
   <span class="classname"><a href="class.dateinterval.html" class="classname">DateInterval</a></span> from the relative parts of the parsed
   string.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-dateinterval.createfromdatestring-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">datetime</code></dt>

     <dd>

      <p class="para">
       A date with relative parts. Specifically, the <a href="datetime.formats.relative.html" class="link">relative formats</a> supported
       by the parser used for <span class="classname"><a href="class.datetimeimmutable.html" class="classname">DateTimeImmutable</a></span>,
       <span class="classname"><a href="class.datetime.html" class="classname">DateTime</a></span>, and <span class="function"><a href="function.strtotime.html" class="function">strtotime()</a></span>
       will be used to construct the DateInterval.
      </p>
      <p class="para">
       To use an ISO-8601 format string like <code class="literal">P7D</code>, you must
       use the constructor.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-dateinterval.createfromdatestring-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a new <span class="classname"><a href="class.dateinterval.html" class="classname">DateInterval</a></span> instance on success,  或者在失败时返回 <strong><code>false</code></strong>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-dateinterval.createfromdatestring-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.2.0</td>
      <td>
       Only the <code class="literal">from_string</code> and
       <code class="literal">date_string</code> properties will be visible when a
       <span class="classname"><a href="class.dateinterval.html" class="classname">DateInterval</a></span> is created with this method.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-dateinterval.createfromdatestring-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2054">
    <p><strong>示例 #1 Parsing valid date intervals</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Each&nbsp;set&nbsp;of&nbsp;intervals&nbsp;is&nbsp;equal.<br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P1D'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'1&nbsp;day'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P2W'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'2&nbsp;weeks'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P3M'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'3&nbsp;months'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P4Y'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'4&nbsp;years'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P1Y1D'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'1&nbsp;year&nbsp;+&nbsp;1&nbsp;day'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P1DT12H'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'1&nbsp;day&nbsp;+&nbsp;12&nbsp;hours'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'PT3600S'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'3600&nbsp;seconds'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2055">
    <p><strong>示例 #2 Parsing combinations and negative intervals</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'62&nbsp;weeks&nbsp;+&nbsp;1&nbsp;day&nbsp;+&nbsp;2&nbsp;weeks&nbsp;+&nbsp;2&nbsp;hours&nbsp;+&nbsp;70&nbsp;minutes'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%d&nbsp;%h&nbsp;%i'</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'1&nbsp;year&nbsp;-&nbsp;10&nbsp;days'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%y&nbsp;%d'</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen"><br />
449 2 70<br />
1 -10<br />
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2056">
    <p><strong>示例 #3 Parsing special relative date intervals</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'last&nbsp;day&nbsp;of&nbsp;next&nbsp;month'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'last&nbsp;weekday'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">);</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程在 PHP 8.2 中的输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(DateInterval)#1 (2) {
  [&quot;from_string&quot;]=&gt;
  bool(true)
  [&quot;date_string&quot;]=&gt;
  string(22) &quot;last day of next month&quot;
}
object(DateInterval)#2 (2) {
  [&quot;from_string&quot;]=&gt;
  bool(true)
  [&quot;date_string&quot;]=&gt;
  string(12) &quot;last weekday&quot;
}
</pre></div>
    </div>
    <div class="example-contents"><p>以上例程在 PHP 8 中的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(DateInterval)#1 (16) {
  [&quot;y&quot;]=&gt;
  int(0)
  [&quot;m&quot;]=&gt;
  int(1)
  [&quot;d&quot;]=&gt;
  int(0)
  [&quot;h&quot;]=&gt;
  int(0)
  [&quot;i&quot;]=&gt;
  int(0)
  [&quot;s&quot;]=&gt;
  int(0)
  [&quot;f&quot;]=&gt;
  float(0)
  [&quot;weekday&quot;]=&gt;
  int(0)
  [&quot;weekday_behavior&quot;]=&gt;
  int(0)
  [&quot;first_last_day_of&quot;]=&gt;
  int(2)
  [&quot;invert&quot;]=&gt;
  int(0)
  [&quot;days&quot;]=&gt;
  bool(false)
  [&quot;special_type&quot;]=&gt;
  int(0)
  [&quot;special_amount&quot;]=&gt;
  int(0)
  [&quot;have_weekday_relative&quot;]=&gt;
  int(0)
  [&quot;have_special_relative&quot;]=&gt;
  int(0)
}
object(DateInterval)#2 (16) {
  [&quot;y&quot;]=&gt;
  int(0)
  [&quot;m&quot;]=&gt;
  int(0)
  [&quot;d&quot;]=&gt;
  int(0)
  [&quot;h&quot;]=&gt;
  int(0)
  [&quot;i&quot;]=&gt;
  int(0)
  [&quot;s&quot;]=&gt;
  int(0)
  [&quot;f&quot;]=&gt;
  float(0)
  [&quot;weekday&quot;]=&gt;
  int(0)
  [&quot;weekday_behavior&quot;]=&gt;
  int(0)
  [&quot;first_last_day_of&quot;]=&gt;
  int(0)
  [&quot;invert&quot;]=&gt;
  int(0)
  [&quot;days&quot;]=&gt;
  bool(false)
  [&quot;special_type&quot;]=&gt;
  int(1)
  [&quot;special_amount&quot;]=&gt;
  int(-1)
  [&quot;have_weekday_relative&quot;]=&gt;
  int(0)
  [&quot;have_special_relative&quot;]=&gt;
  int(1)
}
</pre></div>
    </div>
   </div>
  </p>
 </div>


</div></div></div></body></html>